package com.example.p2pinvest.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.p2pinvest.R;
import com.example.p2pinvest.bean.User;
import com.example.p2pinvest.common.AppNetConfig;
import com.example.p2pinvest.common.BaseActivity;
import com.example.p2pinvest.util.MD5Utils;
import com.example.p2pinvest.util.UIUtils;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import java.nio.charset.StandardCharsets;
import butterknife.BindView;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_title_setting)
    ImageView ivTitleSetting;
    @BindView(R.id.tv_login_number)
    TextView tvLoginNumber;
    @BindView(R.id.et_login_number)
    EditText etLoginNumber;
    @BindView(R.id.rl_login)
    RelativeLayout rlLogin;
    @BindView(R.id.tv_login_pwd)
    TextView tvLoginPwd;
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected void initData() {

    }

    @Override
    protected void initTitle() {
        tvTitle.setText("用户登录");
        ivTitleBack.setVisibility(View.VISIBLE);
        ivTitleSetting.setVisibility(View.INVISIBLE);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @OnClick(R.id.iv_title_back)
    public void back(View view) {
        removeAll();
        gotoActivity(MainActivity.class, null);
    }

    @OnClick(R.id.btn_login)
    public void login(View view) {
        //登录按钮的点击事件
        String number = etLoginNumber.getText().toString().trim();
        String pwd = etLoginPwd.getText().toString().trim();

        if (!TextUtils.isEmpty(number) && !TextUtils.isEmpty(pwd)) {
            String loginUrl = AppNetConfig.LOGIN;
            RequestParams params = new RequestParams();
            params.put("phone", number);
            params.put("password", MD5Utils.MD5(pwd));

            client.post(loginUrl, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    String response = new String(responseBody, StandardCharsets.UTF_8);
                    JSONObject jsonObject = JSON.parseObject(response);
                    boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        String data = jsonObject.getString("data");
                        User user = JSON.parseObject(data, User.class);

                        //保存用户信息
                        saveUser(user);

                        //重新加载界面
                        removeAll();
                        gotoActivity(MainActivity.class, null);

                    } else {
                        Toast.makeText(LoginActivity.this, "用户名不存在或密码不正确", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    UIUtils.toast("联网失败", false);
                }
            });
        } else {
            UIUtils.toast("用户名或密码不能为空", false);
        }
    }

}